package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.SettleAccountsCycle;
import com.ybkj.daijia.server.service.SettleAccountsCycleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * Created by Administrator on 2017/3/2.
 */
@Controller
@RequestMapping("settle")
public class SettleAccountsCycleController {

    @Autowired
    private SettleAccountsCycleService settleAccountsCycleService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list() {
        try {
            Subject subject = SecurityUtils.getSubject();
            Principal principal = (Principal) subject.getPrincipal();
            ModelAndView mv = new ModelAndView();
            mv.setViewName("settleaccountscycle/list");
            Page<SettleAccountsCycle> page = settleAccountsCycleService
                .queryByPage(principal.getCompanyId());
            mv.addObject("page", page);
            return mv;
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加结算周期、上传支付信息
     *
     * @param agency
     * @param bankCyclePeriod
     * @param agencyType
     * @param bankAndLicence
     * @param payType
     * @param serviceRange
     * @param reserveBank
     * @param valid
     * @return
     */
    @RequestMapping("create")
    @ResponseBody
    public Result create(
        @RequestParam("agency") String agency,
        @RequestParam("bankCyclePeriod") Integer bankCyclePeriod,
        @RequestParam("agencyType") String agencyType,
        @RequestParam("bankAndLicence") String bankAndLicence,
        @RequestParam("payType") String payType,
        @RequestParam("serviceRange") String serviceRange,
        @RequestParam("reserveBank") String reserveBank,
        @RequestParam("valid") int valid
    ) {
        Result result = new Result();
        try {

            SettleAccountsCycle sac = new SettleAccountsCycle();
            sac.setAgency(agency);
            sac.setBankCyclePeriod(bankCyclePeriod);
            sac.setAgencyType(agencyType);
            sac.setBankAndLicence(bankAndLicence);
            sac.setPayType(payType);
            sac.setServiceRange(serviceRange);
            sac.setReserveBank(reserveBank);
            sac.setValid(valid);
            Subject subject = SecurityUtils.getSubject();
            Principal principal = (Principal) subject.getPrincipal();
            sac.setCompanyId(principal.getCompanyId());
            settleAccountsCycleService.create(sac);
            result.setSuccess(true);
            result.setResult(sac);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("服务器开小差了,请稍后再试!");
        }
        return result;
    }


    @RequestMapping("delete")
    @ResponseBody
    public Result delete(@RequestParam("id") Long id) {
        Result result = new Result();
        try {
            settleAccountsCycleService.deleteById(id);
            result.setSuccess(true);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("删除失败");
            result.setSuccess(false);
        }
        return result;
    }

}
